﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml;

namespace ImageEditor
{
    public class Map
    {
        private static String _url = "http://www.cdc.gov/flu/weekly/flureport.xml";

        public Map()
        {
            InitializeMapData();
        }

        private void InitializeMapData()
        {
            this.TimePeriods = GetTimePeriodObjects();
            this.CurrentPeriod = this.FinalPeriod;
        }

        private ArrayList GetTimePeriodObjects()
        {
            ArrayList timeList = new ArrayList();
            ArrayList stateList = new ArrayList();
            String[] tagNames = { "timeperiod" };
            String URL = _url;
            ArrayList nodeLists = DataManagement.XMLManager.GetXMLNodeLists(URL, tagNames);
            var mainList = nodeLists[0] as XmlNodeList;
            for (int i = 0; i < mainList.Count; i++)
            {
                var State = mainList.Item(i).ChildNodes;
                for (int j = 0; j < State.Count; j++)
                {
                    var StateDataList = State.Item(j).ChildNodes;
                    var symbol = StateDataList.Item(0).InnerText;
                    var activityLevel = StateDataList.Item(1).InnerText;
                    if (!symbol.Equals("PR") && !symbol.Equals("VI") && !symbol.Equals("DC") && !symbol.Equals("GU"))
                    {
                        stateList.Add(new State(symbol, activityLevel));
                    }
                }
                var attributes = mainList.Item(i).Attributes as XmlAttributeCollection;
                var weekNumber = attributes.Item(0).InnerText;
                var year = attributes.Item(1).InnerText;
                var subtitle = attributes.Item(2).InnerText;
                timeList.Add(new TimePeriod(i, weekNumber, year, subtitle, stateList));
                stateList = new ArrayList();
            }
            return timeList;
        }

        public ArrayList TimePeriods { set; get; }
        public TimePeriod CurrentPeriod { set; get; }
        public TimePeriod FinalPeriod { set { } get { return (TimePeriod)TimePeriods[TimePeriods.Count - 1]; } }
        public TimePeriod NextPeriod
        {
            set { }
            get
            {
                if (CurrentPeriod.Index < TimePeriods.Count - 1)
                {
                    CurrentPeriod = (TimePeriod)TimePeriods[CurrentPeriod.Index + 1];
                    return CurrentPeriod;
                }
                else
                {
                    return CurrentPeriod;
                }
            }
        }
        public TimePeriod PreviousPeriod
        {
            set { }
            get
            {
                if (CurrentPeriod.Index > 0)
                {
                    CurrentPeriod = (TimePeriod)TimePeriods[CurrentPeriod.Index - 1];
                    return CurrentPeriod;
                }
                else
                {
                    return CurrentPeriod;
                }
            }
        }
    }
}
